home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / RUBBER.ZIP / RUBBER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-10  |  4.7 KB  |  165 lines

  1. // compiled with Borland C++ 3.1 large model
  2. // Copyright (c) 1996 David Bollinger
  3. #define RUBBER_C
  4.  
  5. #include <conio.h>   //  for getch()
  6. #include <dos.h>     //  for MK_FP()
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. typedef unsigned char uchar;
  12. typedef unsigned int uint;
  13.  
  14. #include "texture.h"
  15. #include "palette.h"
  16.  
  17. int Initialize(void);
  18. void Terminate(char *msg);
  19. void SetMode(int mode);
  20. void SetPalette(uchar *palette);
  21. void Demo(void);
  22. void RubberSheeting(void);
  23.  
  24. float a,b,c,d,e,f,g,h,i,j,k,l;    // the intercepts & coefficients
  25. char *vram;                       // pointer to video ram
  26.  
  27. //============================================================================
  28. void main(void)
  29.    {
  30.    if (Initialize())
  31.       Demo();
  32.    Terminate("RUBBER.  Copyright (c) 1996 David Bollinger.");
  33.    }
  34.  
  35. //============================================================================
  36. // starting up and shutting down
  37. //----------------------------------------------------------------------------
  38. int Initialize(void)
  39.    {
  40.    SetMode(19);
  41.    SetPalette(palette);
  42.    vram = (uchar *)MK_FP(0xa000,0);
  43.    return 1;
  44.    }
  45.  
  46. void Terminate(char *msg)
  47.    {
  48.    SetMode(3);
  49.    if (msg) puts(msg);
  50.    exit(0);
  51.    }
  52.  
  53. //============================================================================
  54. // graphics stuff
  55. //----------------------------------------------------------------------------
  56. void SetMode(int mode)
  57.    {
  58.    asm   xor   ah, ah
  59.    asm   mov   al, byte ptr mode
  60.    asm   int   10h
  61.    }
  62.  
  63. void SetPalette(uchar *palette)
  64.    {
  65.    asm   les   dx, dword ptr palette
  66.    asm   mov   bx, 0
  67.    asm   mov   cx, 256
  68.    asm   mov   ax, 0x1012
  69.    asm   int   0x10
  70.    }
  71.  
  72. //============================================================================
  73. // very low tech, just prove that it works
  74. //----------------------------------------------------------------------------
  75. #define NumDemos  10
  76. float DemoValues[NumDemos][12] = {
  77.       // 0: linear stretch the 128x128 texture to fill 320x200 screen once
  78.       { 64.0,0,128.0/320.0,0,0,0,  64.0,128.0/200.0,0,0,0,0 },
  79.  
  80.       // 1: same as before but bigger
  81.       { 64.0,0,64.0/320.0,0,0,0,  64.0,64.0/200.0,0,0,0,0 },
  82.  
  83.       // 2: and even bigger, you get the idea...
  84.       { 64.0,0,32.0/320.0,0,0,0,  64.0,32.0/200.0,0,0,0,0 },
  85.  
  86.       // 3: ok, now let's try smaller with tiling
  87.       { 0,0,256.0/320.0,0,0,0,  0,256.0/200.0,0,0,0,0 },
  88.  
  89.       // 4: simple orthogonal rotation by swapping x's and y's
  90.       { 64.0,128.0/200.0,0,0,0,0,  64.0,0,128.0/320.0,0,0,0 },
  91.  
  92.       // 5: shearing effect by mixing & matching x's and y's
  93.       { 0,1,2,0,0,0,  0,2,1,0,0,0 },
  94.  
  95.       // 6: let's add some curves!  (remember X^2+Y^2?)
  96.       { 0,0,1,0,0.001,  0,0,1,0,0,0,0.001 },
  97.  
  98.       // 7: let's add lots of curves!
  99.       { 64.0,0,128.0/320.0,0,0,0.01,  64.0,128.0/200.0,0,0,0,0.01 },
  100.  
  101.       // 8: silly putty
  102.       { 64.0,0,128.0/320.0,0,0.01,0,  64.0,128.0/200.0,0,0,0.01,0 },
  103.  
  104.       // 9: lots of wacky stuff, big smile (and BIG nose!) for the finish! <g>
  105.       { 64.0,0.01,128.0/320.0,0.01,0.01,0.01,  64.0,128.0/200.0,0.01,0.01,0.01,0.01 },
  106.    };
  107.  
  108. void Demo(void)
  109.    {
  110.    int demo;
  111.  
  112.    for (demo=0; demo<NumDemos; demo++)
  113.       {
  114.       a = DemoValues[demo][0];
  115.       b = DemoValues[demo][1];
  116.       c = DemoValues[demo][2];
  117.       d = DemoValues[demo][3];
  118.       e = DemoValues[demo][4];
  119.       f = DemoValues[demo][5];
  120.       g = DemoValues[demo][6];
  121.       h = DemoValues[demo][7];
  122.       i = DemoValues[demo][8];
  123.       j = DemoValues[demo][9];
  124.       k = DemoValues[demo][10];
  125.       l = DemoValues[demo][11];
  126.       RubberSheeting();
  127.       if (getch() == 27) break;  // [Esc] forces quit instead of advance
  128.       }
  129.    }
  130.  
  131. //============================================================================
  132. // here's the fun stuff
  133. //----------------------------------------------------------------------------
  134. void RubberSheeting(void)
  135.    {
  136.    long x, y;
  137.    uint u, v;
  138.    uchar *vptr = vram;
  139.    float y2, abydy2, ghyjy2, yx, x2, ey, ky;
  140.  
  141.    for (y=-100; y<100; y++)
  142.       {
  143.       // this stuff doesn't change in inner loop so precompute it
  144.       y2 = y*y;
  145.       abydy2 = a + b*y + d*y2;
  146.       ghyjy2 = g + h*y + j*y2;
  147.       ey = e*y;
  148.       ky = k*y;
  149.       for (x=-160; x<160; x++)
  150.          {
  151.          // precompute and save a whopping one multiply!
  152.          x2 = x*x;
  153.          // ok, now figure out those texture coordinates!
  154.          u = abydy2 + c*x + ey*x + f*x2;
  155.          u &= 0x7f;
  156.          v = ghyjy2 + i*x + ky*x + l*x2;
  157.          v &= 0x7f;
  158.          // stuff the texture into video memory
  159.          *vptr++ = texture[(v<<7)+u];
  160.          }
  161.       }
  162.    }
  163.  
  164. //============================================================================
  165.